package com.x8.mt.service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Service;

import com.x8.mt.dao.IDatasource_connectinfoDao;
import com.x8.mt.dao.IMetadata_relationtableDao;
import com.x8.mt.dao.IMetadata_tablefieldDao;
import com.x8.mt.dao.IMetadata_tablefieldmappingDao;
import com.x8.mt.dao.ISourcesystemDao;
import com.x8.mt.entity.Datasource_connectinfo;
import com.x8.mt.entity.Metadata_relationtable;
import com.x8.mt.entity.Metadata_tablefield;
import com.x8.mt.entity.Metadata_tablefieldmapping;
import com.x8.mt.entity.Sourcesystem;
import com.x8.mt.entity.TreeNode;

@Service
public class DataAnalysisService {
	@Resource
	IMetadata_tablefieldmappingDao iMetadata_tablefieldmappingDao;
	@Resource
	IMetadata_tablefieldDao iMetadata_tablefieldDao;
	@Resource
	IMetadata_relationtableDao iMetadata_relationtableDao;
	@Resource
	IDatasource_connectinfoDao iDatasource_connectinfoDao;
	@Resource
	ISourcesystemDao iSourcesystemDao;
	
	/**
	 * 
	 * 作者:allen
	 * 时间:2017年11月22日
	 * 作用:血统分析，分析targetfield字段来源
	 */
	public Map<String,Object> pedigreeAnalysis(int targetfieldid){
		Map<String,Object> result = new HashMap<String,Object>();

		Metadata_tablefieldmapping metadata_tablefieldmapping = iMetadata_tablefieldmappingDao.getMetadata_tablefieldmappingBytargetfieldid(targetfieldid);
		Metadata_tablefield metadata_tablefield = iMetadata_tablefieldDao.getMetadata_tablefieldByid(metadata_tablefieldmapping.getSrcfieldid());
		//System.out.println("srcfieldName:"+metadata_tablefield.getName());
		result.put("srcfieldName", metadata_tablefield.getName());
		
		Metadata_relationtable metadata_relationtable = iMetadata_relationtableDao.getMetadata_relationtableByid(metadata_tablefield.getMetadata_relationtableid());
		//System.out.println("srctableName:"+metadata_relationtable.getName());
		result.put("srctableName", metadata_relationtable.getName());
		
		Datasource_connectinfo datasource_connectinfo = iDatasource_connectinfoDao.getDatasource_connectinfoByid(metadata_relationtable.getDatasource_connectinfoid());
		//System.out.println("srcDataBaseName:"+datasource_connectinfo.getDatabasename());
		result.put("srcDataBaseName", datasource_connectinfo.getDatabasename());
		
		Sourcesystem sourcesystem = iSourcesystemDao.getSourcesystemByid(datasource_connectinfo.getParentid());
		//System.out.println("srcSystemName:"+sourcesystem.getName());
		result.put("srcSystemName",sourcesystem.getName());
		
		return result;
	}
	
	/**
	 * 
	 * 作者:GodDispose
	 * 时间:2018年1月21日
	 * 作用:返回数据地图信息
	 */
	public JSONObject getDataMap(){
		int i = 0; 	//节点的起始id
		int type = 1; //结点的类型id
		Queue<TreeNode> queue = new LinkedList<TreeNode>(); // 定义一个队列
		List<Sourcesystem> systems = iSourcesystemDao.getSystemByType("parent");
		for(Sourcesystem system : systems){
			TreeNode e = new TreeNode(i,system.getName(),type,system.getId());
			i++;
			queue.add(e);
		}
		
		JSONObject result = new JSONObject();
		
		JSONArray nodes = new JSONArray();	//结点信息数组
		JSONArray relations = new JSONArray(); //结点关系信息数组
		while(queue.size() != 0){
			int len = queue.size();
			for(int j = 0 ; j < len ; j++){
				TreeNode temp = queue.poll();
				
				JSONObject tempNode = new JSONObject(); //将当前结点信息存到结点信息数组
				tempNode.put("id", temp.getId());
				tempNode.put("label", temp.getName());
				tempNode.put("type", temp.getType());
				nodes.add(tempNode);
				
				try{
					//判断该系统下是否有子节点
					if((iSourcesystemDao.getAllSourcesystemsByParentId(temp.getSourceId()) != null)
							&& iSourcesystemDao.getAllSourcesystemsByParentId(temp.getSourceId()).size()>0){
						List<Sourcesystem> childrenSystems = iSourcesystemDao.getAllSourcesystemsByParentId(temp.getSourceId());
						for(Sourcesystem system : childrenSystems){

							TreeNode e = new TreeNode(i,system.getName(),temp.getType()+1,system.getId());
							queue.add(e); 	//将结点信息加入队列

							
							JSONObject tempRelation = new JSONObject(); //将当前结点信息存到结点关系信息数组
							tempRelation.put("sourceID", temp.getId());
							tempRelation.put("targetID", i);
							relations.add(tempRelation);
							i++;
							
						}
					}
				}catch(Exception e){
					return result;
				}						
			}
		}
		
		result.put("nodes", nodes);
		result.put("relations", relations);
		
		return result;
	}
}
